import * as dto from './DTO';
import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import stringify from 'Shared/utils/stringify';
import {formatMessage} from './intl';

const baseUrl = '/parts/api/v1';

const initDesc = formatMessage({
    id: 'api.message.initDesc',
    defaultMessage: '初始化界面'
});
const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询备件通知通告'
});
const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询备件通知通告详情'
});
const getStatisDetailsDesc = formatMessage({
    id: 'api.message.getStatisDetailsDesc',
    defaultMessage: '查询签收回复统计'
});
const newOrderDesc = formatMessage({
    id: 'api.message.newOrderDesc',
    defaultMessage: '新增备件通知通告'
});
const modifyDesc = formatMessage({
    id: 'api.message.modifyDesc',
    defaultMessage: '修改备件通知通告'
});
const signforDesc = formatMessage({
    id: 'api.message.signforDesc',
    defaultMessage: '签收备件通知通告'
});
const replyDesc = formatMessage({
    id: 'api.message.replyDesc',
    defaultMessage: '回复备件通知通告'
});
const canceltDesc = formatMessage({
    id: 'api.message.canceltDesc',
    defaultMessage: '取消发布备件通知通告'
});
const releaseDataDesc = formatMessage({
    id: 'api.message.releaseDataDesc',
    defaultMessage: '发布备件通知通告'
});
const abandonDataDesc = formatMessage({
    id: 'api.message.abandonDataDesc',
    defaultMessage: '作废备件通知通告'
});
const getNoticeProgramListDesc = formatMessage({
    id: 'api.message.getNoticeProgramListDesc',
    defaultMessage: '查询备件通知通告栏目'
});
const abandonNoticeProgramDataDesc = formatMessage({
    id: 'api.message.abandonNoticeProgramDataDesc',
    defaultMessage: '作废备件通知通告栏目'
});
const getNoticeProgramDetailDesc = formatMessage({
    id: 'api.message.getNoticeProgramDetailDesc',
    defaultMessage: '查询备件通知通告栏目详情'
});
const modifyNoticeProgramOrderDesc = formatMessage({
    id: 'api.message.modifyNoticeProgramOrderDesc',
    defaultMessage: '修改备件通知通告栏目详情'
});
const newNoticeProgramOrderDesc = formatMessage({
    id: 'api.message.newNoticeProgramOrder',
    defaultMessage: '新增备件通知通告栏目信息'
});
const getDealerDesc = formatMessage({
    id: 'api.message.getDealerDesc',
    defaultMessage: '查询总代信息'
});
const getListTreeDesc = formatMessage({
    id: 'api.getListTreeDesc',
    defaultMessage: '查询大区树'
});
const getRegionDesc = formatMessage({
    id: 'api.getRegionDesc',
    defaultMessage: '查询国际区域列表'
});
const importTempDesc = formatMessage({
    id: 'api.importTempDesc',
    defaultMessage: '下载导入清单模板'
});
//初始化
export const getInitData = () => request(`${baseUrl}/ui/notice/init`, null, dto.InitDataStruct)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, initDesc));
// 查询订单列表
export const getList = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/notices/forCurrentUser?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};
// 查询详情
export const getDetail = id =>
    request(`${baseUrl}/notices/${id}`, null, dto.Detail)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));
// 查询签收回复统计
export const getStatisDetails = () =>
    request(`${baseUrl}/noticePrograms/statis`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getStatisDetailsDesc));
//新增
export const newOrder = data => request(`${baseUrl}/notices`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, newOrderDesc));
//修改
export const modifyOrder = (id, data) => request(`${baseUrl}/notices/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, modifyDesc));
//发布
export const releaseData = (id, data) => request(`${baseUrl}/notices/${id}/release`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, releaseDataDesc));
//取消发布
export const cancelData = (id, data) => request(`${baseUrl}/notices/${id}/cancel`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, canceltDesc));
//作废
export const abandonData = (id, data) => request(`${baseUrl}/notices/${id}/abandon`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, abandonDataDesc));
//签收
export const signforData = (id, data) => request(`${baseUrl}/notices/${id}/signfor`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, signforDesc));
//回复
export const replyData = (id, data) => request(`${baseUrl}/notices/${id}/reply`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, replyDesc));
//导入dealer模板
export const dealerImportTemp = () => request(`${baseUrl}/dealers/forNoticeImport`)
    .then(handleFetchResultNotification(REQUEST_TYPE.submit, importTempDesc)).then(res => res.data);
// 查询栏目列表
export const getNoticeProgramList = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/noticePrograms?${queryString}`, null, dto.NoticeProgramList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getNoticeProgramListDesc));
};
//查询栏目详情
export const getNoticeProgramsDetail = id =>
    request(`${baseUrl}/noticePrograms/${id}`, null, dto.NoticeProgramDetail)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getNoticeProgramDetailDesc));
//作废栏目信息
export const abandonNoticeProgramData = (id, data) => request(`${baseUrl}/noticePrograms/${id}/abandon`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, abandonNoticeProgramDataDesc));
//新增栏目信息
export const newNoticeProgramOrder = data => request(`${baseUrl}/noticePrograms`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, newNoticeProgramOrderDesc));
//修改栏目信息
export const modifyNoticeProgramOrder = (id, data) => request(`${baseUrl}/noticePrograms/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, modifyNoticeProgramOrderDesc));
// 查询总代
export const getDealers = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forSubDealer?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealerDesc));
};
// 根据国际区域查询大区
export const getMarketDepartments = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/marketDepartments/forSubDealer?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListTreeDesc));
};
// 查询国际区域列表
export const getRegions = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/areaTileds/forSubDealer?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getRegionDesc));
};
//根据条件查询总代子公司
export const getDealerSimple = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forSubDealer/simple?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealerDesc));
};
// 查询人员信息
export const getPersonnels = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/personnels/simple?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};
